Ext.define('AmpleHarvest.objects.results.results',
{
	extend		: 'Ext.grid.Panel',
	alias		: 'widget.ampleharvest-objects-results-results',
	title		: 'Results',
	requires	: 
		[
	    	'Ext.ux.PreviewPlugin'
	    ],
	bubbleEvents	:
		[
			'processingFinished',
			'openItem'
		],
	autoScroll	: true,
//	viewConfig: {
//        id: 'gv', // ID is reset to dynamic in initComponent
//        trackOver: false,
//        stripeRows: false,
//        plugins: [{
//            ptype: 'preview',
//            //TODO : need to find a way to make the bodyField dynamic
//            bodyField: 'summary',
//            expanded: true,
//            pluginId: 'preview'
//        }]
//    },
	
    setConfigOptions	: function(str_field)
    {
    	this.viewConfig.plugins[0].bodyField = str_field;
    },
    
    //TODO : look into config constructor
    constructor: function(config) {

    	var str_summary_field = !Ext.isString(config.previewField) ? 'summary' : config.previewField;
    	
    	this.viewConfig = {
    	        id: 'gv', // ID is reset to dynamic in initComponent
    	        trackOver: false,
    	        stripeRows: false,
    	        plugins: [{
    	            ptype: 'preview',
    	            //TODO : need to find a way to make the bodyField dynamic
    	            bodyField: str_summary_field,
    	            expanded: true,
    	            pluginId: 'preview'
    	        }]
    	    }
    	
    	this.callParent(arguments);
    },
    
    initComponent	: function()
	{    	
    	this.viewConfig.id = 'gv'+(((1+Math.random())*0x10000)|0).toString(16).substring(1);
    	
    	var pluginExpanded = true;
    	
    	this.dockedItems = [
		                	   {
		                		   	xtype		: 'pagingtoolbar',
		  							store		: this.getStore(),
		                	   		dock		: 'bottom',
		                	   		uuid		: this.viewConfig.id,
		                	   		displayInfo	: true,
		                	   		items:[
		                	                '-', 
		                	                {
			                	                text: 'Show Preview',
			                	                pressed: pluginExpanded,
			                	                enableToggle: true,
			                	                toggleHandler: function(btn, pressed) {
			                	                    var preview = Ext.getCmp(this.ownerCt.uuid).getPlugin('preview');
			                	                    preview.toggleExpanded(pressed);
			                	                }
		                	            	}
		                	   ]
		                	   }];
    	
    	this.callParent();
	
		
		this.getStore().on('datachanged', function(){ this.fireEvent('processingFinished')}, this);// this.reportFinished, this);
		this.on('itemdblclick', function(model, record, index) { this.fireEvent('openItem', model, record, index, this) });
		if(Ext.isString(this.m_str_exportPath))
		{
			this.addListener({
	           'itemcontextmenu' : this.showContextMenuRightClick
			});
		}
		
	},
	
	showContextMenuRightClick: function(grid, record, item, index, event, opts)
    {
    	event.preventDefault();
    	
    	var exportMenu = new Ext.menu.Menu({
    		items: [{
    			scope: this,
                text: 'Export',
                handler: function() {
                	open(this.m_str_exportPath);
                }
    		}]
    	});
    	exportMenu.showAt(event.getXY());
    },

	reportFinished	: function()
	{
		this.fireEvent('processingFinished');
	},
	
	loadQuery	: function(str_query)
	{
//		console.log(str_query);
		
		var obj_query = new Object();
		obj_query.query = str_query;
		obj_query.limit	= this.maxLimit;
		this.getStore().set_ExtraParams(obj_query);
		this.getStore().load();
	},
	
	clearQuery	: function()
	{
		this.getStore().removeAll();
		this.reportFinished();
	}
});